//
//  RadiolistViewController.m
//  SunXi
//
//  Created by dllo on 15/11/28.
//  Copyright © 2015年 大远. All rights reserved.
//

#import "RadiolistViewController.h"

@interface RadiolistViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation RadiolistViewController{
    RadiolistheadView *_headView;
    UITableView *_tabelView;
    NSMutableArray *_arr;
    NSInteger loadNumber;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    SV.backgroundColor = [UIColor whiteColor];;
    CGFloat hei =  WIDTH / 640  * 280 + 100;
    _headView= [[RadiolistheadView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, hei)];
    
    _tabelView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [SV addSubview:_tabelView];
    [_tabelView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(SV).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    _tabelView.delegate = self;
    _tabelView.dataSource = self;
    _tabelView.tableHeaderView = _headView;
    _tabelView.rowHeight = 80;
    _tabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tabelView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        loadNumber += 10;
        [self loadData];
    }];
    [self createData];

}
- (void)createData{
    NSString *radioUrl = @"http://api2.pianke.me/ting/radio_detail";
    NSString *para = [NSString stringWithFormat:@"radioid=%@&start=0&client=2&limit=10",self.radioid];
    [DyAFnetWorking cachePostWithURL:radioUrl WithbodyStr:para success:^(id result) {
        _arr = [RadioModel baseModelByArr:result[@"data"][@"list"]];
        
        for (RadioModel *tempmodel in _arr) {
            tempmodel.title = tempmodel.userinfo[@"title"];
        }
  
        if (_arr.count != 10) {
            [_tabelView.mj_footer endRefreshingWithNoMoreData];
        }
        //给头部
        RadioModel *headModel = [[RadioModel alloc]init];
        [headModel setValuesForKeysWithDictionary:result[@"data"][@"radioInfo"]];
        [_headView loadHeadView:headModel];
        self.navigationItem.title = headModel.title;
        [_tabelView reloadData];

    } failure:^(id nonet) {
        
        
    }];
;
    
}
- (void)loadData{
    NSString *radioUrl = @"http://api2.pianke.me/ting/radio_detail_list";
    NSString *str = [NSString stringWithFormat:@"%ld",(long)loadNumber];
    NSString *para = [NSString stringWithFormat:@"radioid=%@&start=%@&client=2&limit=10",self.radioid,str];
    [DyAFnetWorking cachePostWithURL:radioUrl WithbodyStr:para success:^(id result) {
        NSMutableArray *tempArr = [NSMutableArray array];
        tempArr = [RadioModel baseModelByArr:result[@"data"][@"list"]];
        for (RadioModel *tempmodel in tempArr) {
            tempmodel.title = tempmodel.userinfo[@"title"];
        }
        [_arr addObjectsFromArray:tempArr];
        if (tempArr.count == 10) {
            [_tabelView.mj_footer endRefreshing];
        }else{
            [_tabelView.mj_footer endRefreshingWithNoMoreData];
        }
        [_tabelView reloadData];
        
    } failure:^(id nonet) {
        
        
    }];
    
    
    
    
    
    
 
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RadiolistTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RadiolistTableViewCell"];
    if (!cell) {
        cell = [[RadiolistTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"RadiolistTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = _arr[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    RadioplayViewController *playVC = [[RadioplayViewController alloc]init];
    [RadioSavemodel shareSingleton].playlistArr = _arr;
    [RadioSavemodel shareSingleton].playNumber = indexPath.row;
    [self.navigationController pushViewController:playVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (IBAction)presentLeftMenuViewController:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if ([RadioSavemodel shareSingleton].playlistArr.count > 0) {
        //右边按钮
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"zhuanpan"] style:UIBarButtonItemStylePlain target:self action:@selector(pushplaycon)];
        self.navigationItem.rightBarButtonItem.tintColor = BUTTONCOLOR;
        
    }
}


- (void)pushplaycon{
    RadioplayViewController *VC = [[RadioplayViewController alloc]init];
    [self.navigationController pushViewController:VC animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
